package com.lz.collection.specification;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数组转list {@link Arrays} 不能进行写操作，只能get
 *
 * @author: li
 * @date: 2019/1/10
 */
public class Array2ListTest {
    @Test
    public void array2List() {

        String[] strs = {"1", "a", "b"};
        List<String> strings = Arrays.asList(strs);
        // 报错
        // strings.add("c");
        // strings.remove("b");
        System.out.println(Arrays.toString(strs));

        //------正确做法---
        List<String> list2 = new ArrayList<>(Arrays.asList(strs));
        list2.add("c");
        list2.forEach(System.out::printf);
    }

    @Test
    public void list2Array1() {
        List<String> list = new ArrayList<String>(3);
        list.add("a");
        list.add("b");
        list.add("c");

        Object[] objects = list.toArray();
        System.out.println(Arrays.toString(objects));


    }

    @Test
    public void test() {
        List<String> list = new ArrayList<String>(3);
        list.add("a");
        list.add("b");
        list.add("c");
        // 导致泛型丢失
        Object[] objects = list.toArray();
        System.out.println(Arrays.toString(objects));
        // array2容量小于list null
        String[] array2 = new String[2];
        String[] strings = list.toArray(array2);
        System.out.println(Arrays.asList(array2) + "" + Arrays.asList(strings));
        // 推荐使用 arrary3== list.size()
        String[] array3 = new String[3];
        String[] strings3 = list.toArray(array3);
        System.out.println(Arrays.asList(array3));

    }

    @Test
    public void pkTime() {
        final int COUNT = 100 * 100 * 100;
        List<Double> list = new ArrayList<>(COUNT);
        long s1 = 0;
        long s2 = 0;
        long s3 = 0;
        for (int i = 0; i < COUNT/100; i++) {
            list.add(i * 1.0);
            long start = System.nanoTime();
            Double[] notEnoughArray = new Double[COUNT - 1];
            list.toArray(notEnoughArray);
            long middle = System.nanoTime();
            Double[] equalArray = new Double[COUNT];
            list.toArray(equalArray);
            long middle2 = System.nanoTime();
            Double[] doubleArray = new Double[COUNT *2];
            list.toArray(doubleArray);
            long end = System.nanoTime();
            long notEnoughArrayTime = middle - start;
            long equalArrayTime = middle2 - middle;
            long doubleArrayTime = end - middle2;
            s1 += notEnoughArrayTime;
            s2 += equalArrayTime;
            s3 += doubleArrayTime;
            // System.out.println("1:" + notEnoughArrayTime / (1000.0 * 1000.0));
            // System.out.println("2:" + equalArrayTime / (1000.0 * 1000.0));
            // System.out.println("3:" + doubleArrayTime / (1000.0 * 1000.0));
            // System.out.println("============================");
        }
        System.out.println(s1/COUNT*100);
        System.out.println(s2/COUNT*100);
        System.out.println(s3/COUNT*100);

    }
}